/**
 * 
 */
package com.cua24.donkeyroll;

/**
 * @author dikaberg
 *
 */
import java.io.IOException;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;

public class GameActivity extends BaseGameActivity {

	private BoundCamera camera;
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) { //*//
        getMenuInflater().inflate(R.menu.game, menu);
        return true;
    }


	@Override
	public EngineOptions onCreateEngineOptions() {//*//
		camera = new BoundCamera(0,0,800,480);
		EngineOptions engineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED,new FillResolutionPolicy(),this.camera);
		engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
		engineOptions.getRenderOptions().getConfigChooserOptions().setRequestedMultiSampling(true);
		engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
		return engineOptions;
	}

	
	@Override
	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback)throws IOException {//*//
		ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
		pOnCreateResourcesCallback.onCreateResourcesFinished();
		
	}


	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws IOException {//*//
		SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
	}
	

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback)
			throws IOException {//*//
		mEngine.registerUpdateHandler(new TimerHandler(2f,new ITimerCallback(){
			public void onTimePassed(final TimerHandler pTimerHandler){
				mEngine.unregisterUpdateHandler(pTimerHandler);
				SceneManager.getInstance().createMenuScene();
			}
		}));
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}
    
	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions){//*//
		return new LimitedFPSEngine(pEngineOptions,60);
	}

	/*@Override
	protected void onDestroy(){ //
		super.onDestroy();
	        System.exit(0);	
	}*/
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){//*//
	    if (keyCode == KeyEvent.KEYCODE_BACK){
	        SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
	    }
	    return false; 
	}
	
}
